$(function () {
    ImporInfor();
    // 页面主要元素
    var first = $('#first'),
        second = $('#second'),
        verification = true,    // 验证成功
        onOffVerify = true,     //是否允许再次发送验证码
        useOldPhone = Number(window.Cookie.getCookie('phone')) || '',
        // useid = Number(window.Cookie.getCookie('uuid')) || '',
//      useOldPhone = Number(window.Cookie.getCookie('phone')) || '',
	
        oldInput = $('#oldPhone'),
        usenewPhone = '',
        oldCode = '',
        // 手机合法
        piPwd_reg = '',
        urlType = '',
        dataType = '';
        
        console.log(useOldPhone)

    // 初始化 页面输入框
    oldInput.attr({'placeholder':useOldPhone});

    // 旧手机发送验证码
    $('#oldCode').click(function () {
        urlType = '/phone/sendPhoneVerifCode';
        dataType = "phone="+ useOldPhone;
        if(onOffVerify == true){
            askPostData(urlType);
        }else {
            return false;
        }
    })
    $('body').on('scroll',function () {
        return false;
    })
    // 新手机发送验证码
    $('#newCode').click(function () {
        urlType = '/phone/sendPhoneVerifCode';
        dataType = "phone="+ usenewPhone;
        if(onOffVerify == true){
            askPostData(urlType,1);
        }else {
            return false;
        }
    })
    // 完成验证
    $('#second header h5 a').click(function () {
        var newcode = $('#second .verify-code input').val();
        if (!newcode || typeof Number(newcode) !== 'number'){
            $('#newuserCode').text('(验证码不正确)')
            $('#newuserCode').css({
                'display':'block',
                'color':'red'
            })
            return;
        }
        fixPhone(usenewPhone, newcode,function (data) {
           if(data.success){
//         		Cookie.setCookie('phone',usenewPhone)
               window.location.href = 'myAccount.html'
           }else {
               $('#newuserCode').text('(验证码不正确)')
               $('#newuserCode').css({
                   'display':'block',
                   'color':'red'
               })
           }
        })
    })

    // 下一步按钮
    $('#next').click(function () {
        oldCode = $('#first .verify-code input').val();
        if(!oldCode || typeof Number(oldCode) !== "number"){
            $('#userCode').text('(验证码不正确)')
            $('#userCode').css({
                'display':'block',
                'color':'red'
            })
            return
        }
        $('#newCode').attr({
            disabled:true
        }).css({
            backgroundColor:'rgb(204, 204, 204)'
        })
        askServer(useOldPhone, oldCode,function (data) {
            if(data.success === true){
                onOffVerify = true;
                first.animate({left:'-100%'},500).hide(1000);
                second.show().animate({left:'0'},500)
            }else{
                $('#userCode').text('(验证码不正确)')
                $('#userCode').css({
                    'display':'block',
                    'color':'red'
                })
            }
        })
    })

    // 返回按钮
    $('#goFirst').click(function () {
        window.location.href = 'myAccount.html'
    })
    $('#newPhoneNumber').on('input', function () {
        piPwd_reg = /^1[34578]\d{9}/.test($('#newPhoneNumber').val());
        if(piPwd_reg){
            askPostDate2($(this).val())
        }else {

            $('#newuserPhone').text('('+ '手机号的格式错误' +')')
            $('#newuserPhone').css({
                'display':'block',
                'color':'red'
            })
            $('#newCode').attr({disabled:false}).css({
                backgroundColor:'rgb(204, 204, 204)'
            })
        }

    })

    // 更改手机号
    function fixPhone(phone, code,fn) {
        var params = 'phone='+ phone+'&phoneVerifCode='+code +"&" + Infornation;
        $.ajax({
            url:host+'/myhome/account/modifyPhone',
            type:'post',
            dataType:'json',
            data:params,
            success:function (data) {
                console.log(data)
                fn(data)
            }
        })
    }
    // 验证手机的验证码是否正确
    function askServer(phone, code,fn) {
        var params = 'phone='+ phone+'&phoneVerifCode='+code +"&" + Infornation;
        console.log(params)
        $.ajax({
            url:host+'/myhome/account/verifyPhone',
            type:'post',
            dataType:'json',
            data:params,
            success:function (data) {
                fn(data)
            }
        })
    }
    // 验证旧手机验证码按钮点击函数
    function askPostData(num,bit){
        bit = bit || 0;
        $.ajax({
            url:host + num,
            type:'post',
            data:dataType,
            success:function(res){
                if(res.success == true){
                    onOffVerify = false;
                    //11/18 New created
                    var countdown = 120;
                    $('.content button').eq(bit).text('倒计时 ' + countdown + 's');
                    $('.content button').eq(bit).attr({
                        disabled:true
                    }).css({backgroundColor:'rgb(204, 204, 204)'})
                    countdown--;
                    var timer = setInterval(function(){
                        $('.content button').eq(bit).text('倒计时 ' + countdown + 's');
                        countdown--;
                        if(countdown < 0){
                            onOffVerify = true;
                            clearInterval(timer);
                            $('.content button').eq(bit).attr({
                                disabled:false
                            }).css({backgroundColor:'rgb(49, 162, 255)'})
                            $('.content button').eq(bit).text('获取验证码');
                        }
                    },1000);

                }else{
                    if (res.message.indexOf('一分钟') !== -1){
                        $('.content button').eq(bit).text('请在一分钟之后尝试');
                    }
                }
            }
        });
    }
    // 验证新手机是否可用
    function askPostDate2(val){
        dataType = "phone="+ val;
        $.ajax({
            url:host +"/register/validatePhone",
            type:'post',
            data:dataType,
            success:function(res){
                console.log(res)
                if(res.success){
                    usenewPhone = val;
                    $('#newuserPhone').text('('+ '此手机号可用' +')')
                    $('#newuserPhone').css({
                        'display':'block',
                        'color':'green'
                    })
                    if(onOffVerify){
                        $('#newCode').attr({disabled:false}).css({
                            backgroundColor:'rgb(49, 162, 255)',
                        })
                    }

                }else{
                    $('#newuserPhone').text('('+ res.message +')')
                    $('#newuserPhone').css({
                        'display':'block',
                        'color':'red'
                    })
                }
            }
        })
    }
})


